package com.lxblyw.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.lxblyw.entity.Task;
import com.lxblyw.util.DBHelp;
import com.lxblyw.util.RowMapper;

public class TaskDao {
	private DBHelp<Task> dbhelp = new DBHelp<Task>();
	
	private class TaskRowMapper implements RowMapper<Task> {
		public Task mapperRow(ResultSet rs) throws SQLException {
			Task task = new Task();
			task.setId(rs.getString("id"));
			task.setName(rs.getString("name"));
			task.setDesc(rs.getString("desc"));
			task.setState(rs.getString("state"));
			task.setLevel(rs.getString("level"));
			task.setBegintime(rs.getString("begintime"));
			task.setEndtime(rs.getString("endtime"));
			task.setCreatetime(rs.getString("createtime"));
			task.setRate(rs.getString("rate"));
			task.setGoalid(rs.getString("goalid"));
			task.setEmployeeid(rs.getString("employeeid"));
			
			return task;
		}
		
	}

	public List<Task> findByGoalId(String goalid) {
		String sql = "select * from t_task where goalid = ?";
		return dbhelp.executeQueryForList(sql, new TaskRowMapper(), goalid);
	}

	public void save(Task task) {
		String sql = "insert into t_task(id,`name`,`desc`,state,`level`,begintime,endtime,createtime,goalid,employeeid) values(?,?,?,?,?,?,?,?,?,?)";
		dbhelp.executeSQL(sql, task.getId(),task.getName(),task.getDesc(),task.getState(),task.getLevel(),task.getBegintime(),task.getEndtime(),task.getCreatetime(),task.getGoalid(),task.getEmployeeid());
	}

	public Task findById(String id) {
		String sql = "select * from t_task where id = ?";
		return dbhelp.executeQueryForObject(sql, new TaskRowMapper(), id);
	}

	public void update(String name, String desc, String begintime,
			String endtime, String state, String level, String manager,
			int rate,String id) {
		String sql = "update t_task set `name` = ?,`desc` = ?,begintime = ?,endtime = ?,state = ?,`level` = ?,employeeid = ?,rate = ? where id = ?";
		dbhelp.executeSQL(sql, name,desc,begintime,endtime,state,level,manager,rate,id);
	}

	public List<Task> findByEmployeeId(String id) {
		String sql = "select * from t_task where employeeid = ?";
		return dbhelp.executeQueryForList(sql, new TaskRowMapper(), id);
	}
}
